<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日文转平假名</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            display: flex;
            width: 80%;
            height: 60%;
        }

        .input-area {
            flex: 1;
            padding: 10px;
        }

        textarea {
            width: 100%;
            height: 100%;
            resize: none;
        }

        .button-area {
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 10px;
        }

        button {
            margin: 5px;
            padding: 10px 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="input-area">
            <textarea id="inputText" placeholder="请输入日文文本"></textarea>
        </div>
        <div class="button-area">
            <button id="convertButton">转换</button>
            <button id="clearButton">清空</button>
        </div>
        <div class="input-area">
            <textarea id="outputText" readonly></textarea>
        </div>
    </div>

    <!-- 引入 kuroshiro 和 kuromoji 库 -->
    <script src="./node_modules/kuroshiro/dist/kuroshiro.min.js"></script>
    <script src="./node_modules/kuromoji/build/kuromoji.js"></script>
    <script>
        // 初始化 kuroshiro
        const kuroshiro = new Kuroshiro();
        const inputText = document.getElementById('inputText');
        const outputText = document.getElementById('outputText');
        const convertButton = document.getElementById('convertButton');
        const clearButton = document.getElementById('clearButton');

        // 初始化 kuroshiro 解析器
        async function initKuroshiro() {
            await kuroshiro.init(new Kuroshiro.Converter({
                dicPath: './node_modules/kuromoji/dict/'
            }));
        }

        // 转换函数
        async function convertText() {
            const text = inputText.value;
            if (text) {
                const result = await kuroshiro.convert(text, { to: 'hiragana' });
                outputText.value = result;
            }
        }

        // 清空函数
        function clearText() {
            inputText.value = '';
            outputText.value = '';
        }

        // 初始化 kuroshiro
        initKuroshiro();

        // 绑定按钮事件
        convertButton.addEventListener('click', convertText);
        clearButton.addEventListener('click', clearText);
    </script>
</body>

</html>